<?php
/**
 * Created by .
 * User: luogan
 * Date: 2022/1/13
 */

namespace app\admin\validate;


use app\model\AdminModel;

class AdminValidate extends BaseValidate
{
    protected $rule = [
        'id' => 'require|isPositiveInteger',
        'username' => 'require|max:20',
        'password' => 'require|max:25',
        'nickname' => 'max:255',
        'role' => 'max:25',
        'dept_id' => 'isPositiveInteger',
        'status' => 'between:0,1',
        'sex' => 'between:0,1',
        'mobile' => 'mobile',
        'email' => 'email',
        'create_time' => 'dateFormat:Y-m-d H:i:s',
        'page' => 'isPositiveInteger',
        'size' => 'isPositiveInteger',
    ];

    protected $message = [
    ];

    protected $scene = [
        'login' => ['username', 'password'],
        'list' => ['page','size','mobile','email','dept_id','nickname','status'],
        'add' => ['nickname','username', 'password','status','role','dept_id','mobile','email','sex'],
        'info' => ['id'],
        'del' => ['id'],
        'edit' => ['id','username','status','role','dept_id','mobile','email','sex'],
    ];

    /**
     * 修改create验证场景规则
     * @return AdminValidate
     */
    public function sceneAdd()
    {
        return $this->only($this->scene[$this->currentScene])->append('username', 'checkUserName');
    }

    public function sceneStatus()
    {
        return $this->only(['id','status'])->append('status', 'require');
    }

    /**
     * 检测管理员用户名是否存在
     * @param $value
     * @param string $rule
     * @param string $data
     * @param string $field
     * @return bool|string
     */
    protected function checkUserName($value, $rule='', $data='', $field='')
    {
        $result = AdminModel::where('username',$value)->count();
        if ($result>0){
            return $value . '用户名已存在';
        }
        return true;
    }
}
